/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.web.shop;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkShopCoupon;
import com.thinkgem.jeesite.modules.hk.shop.service.shop.HkShopCouponService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkShopLuckyBurstCoupon;
import com.thinkgem.jeesite.modules.hk.shop.service.shop.HkShopLuckyBurstCouponService;

/**
 * 幸运爆款优惠券Controller
 *
 * @author 李欣荣
 * @version 2018-10-11
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/shop/hkShopLuckyBurstCoupon")
public class HkShopLuckyBurstCouponController extends BaseController {

    @Autowired
    private HkShopLuckyBurstCouponService hkShopLuckyBurstCouponService;

    @Autowired
    private HkShopCouponService hkShopCouponService;

    @ModelAttribute
    public HkShopLuckyBurstCoupon get(@RequestParam(required = false) String id) {
        HkShopLuckyBurstCoupon entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = hkShopLuckyBurstCouponService.get(id);
        }
        if (entity == null) {
            entity = new HkShopLuckyBurstCoupon();
        }
        return entity;
    }

    @RequiresPermissions("shop:shop:hkShopLuckyBurstCoupon:view")
    @RequestMapping(value = {"list", ""})
    public String list(HkShopLuckyBurstCoupon hkShopLuckyBurstCoupon, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<HkShopLuckyBurstCoupon> page = hkShopLuckyBurstCouponService.findPage(new Page<HkShopLuckyBurstCoupon>(request, response), hkShopLuckyBurstCoupon);
        model.addAttribute("page", page);
        return "hk/shop/shop/hkShopLuckyBurstCouponList";
    }

    @RequiresPermissions("shop:shop:hkShopLuckyBurstCoupon:view")
    @RequestMapping(value = "form")
    public String form(HkShopLuckyBurstCoupon hkShopLuckyBurstCoupon, Model model) {
        model.addAttribute("hkShopLuckyBurstCoupon", hkShopLuckyBurstCoupon);
        return "hk/shop/shop/hkShopLuckyBurstCouponForm";
    }

    @RequiresPermissions("shop:shop:hkShopLuckyBurstCoupon:edit")
    @RequestMapping(value = "save")
    public String save(HkShopLuckyBurstCoupon hkShopLuckyBurstCoupon, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, hkShopLuckyBurstCoupon)) {
            return form(hkShopLuckyBurstCoupon, model);
        }

        /**
         * 根据优惠券ID获取商品ID、优惠券生效时间、优惠券过期时间
         */
        String shopPouponId = hkShopLuckyBurstCoupon.getCoupon().getId();
        if (StringUtils.isBlank(shopPouponId)) {
            addMessage(redirectAttributes, "保存新人专享优惠券失败");
            return "redirect:" + Global.getAdminPath() + "/shop/shop/hkShopNewVipCoupon/?repage";
        } else {
            HkShopCoupon hkShopCoupon = hkShopCouponService.get(shopPouponId);
            hkShopLuckyBurstCoupon.setProductId(hkShopCoupon.getProduct().getId());    //商品ID
            hkShopLuckyBurstCoupon.setDiscount(hkShopCoupon.getDiscount());  //折扣
            hkShopLuckyBurstCouponService.save(hkShopLuckyBurstCoupon);
            addMessage(redirectAttributes, "保存幸运爆款优惠券成功");
            return "redirect:" + Global.getAdminPath() + "/shop/shop/hkShopLuckyBurstCoupon/?repage";
        }


    }

    @RequiresPermissions("shop:shop:hkShopLuckyBurstCoupon:edit")
    @RequestMapping(value = "delete")
    public String delete(HkShopLuckyBurstCoupon hkShopLuckyBurstCoupon, RedirectAttributes redirectAttributes) {
        hkShopLuckyBurstCouponService.delete(hkShopLuckyBurstCoupon);
        addMessage(redirectAttributes, "删除幸运爆款优惠券成功");
        return "redirect:" + Global.getAdminPath() + "/shop/shop/hkShopLuckyBurstCoupon/?repage";
    }

}